package sys

import (
	"net"
)

func IP() (string, error) {
	netInterfaces, err := net.Interfaces()
	if err != nil {
		return "", err
	}

	for i := 0; i < len(netInterfaces); i++ {
		if (netInterfaces[i].Flags & net.FlagUp) != 0 {
			addrs, _ := netInterfaces[i].Addrs()

			for _, add := range addrs {
				if ipNet, ok := add.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
					if ipNet.IP.To4() != nil {
						ip := ipNet.IP.String()
						return ip, nil
					}
				}
			}

		}
	}
	return "", nil
}
